/**
 * 商品类目中间件
 * @type {GoodsCategory}
 */
const GoodsCategory = require('../model/goods_category');

module.exports = {

  /**
   * 插入商品类目的中间件
   * @param request
   * @param response
   * @param next
   */
  sGoodsCategory: (request, response, next) => {
    let {name, describe, root_category_id, user_username, index} = request.body
    let good_category = {
      name,
      describe,
      root_category_id,
      user_username,
      index,
    }
    GoodsCategory.setGoodsCategory(good_category)
      .then(result => {
        request.add_gcid = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 删除商品类目
   * @param request
   * @param response
   * @param next
   */
  dGoodsCategory: (request, response, next) => {
    let {id} = request.body
    GoodsCategory.deleteGoodsCategory(id)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 修改商品类目的中间件
   * @param request
   * @param response
   * @param next
   */
  uGoodsCategory: (request, response, next) => {
    let {goods_category} = request.body
    GoodsCategory.updateGoodsCategory(goods_category)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定商品类目数量的中间件
   * @param request
   * @param response
   * @param next
   */
  gGoodsCategoryCount: (request, response, next) => {
    let {id, name_describe, username, root_category_id} = request.body;
    console.log('--------------------id=' + id + ';name_describe=' + name_describe + ';username=' + username +
      ';root_category_id=' + root_category_id + ';-------------------------');
    GoodsCategory.getGoodsCategoryCount(id, name_describe, username, root_category_id)
      .then(result => {
        request.recordCountGoodsCategory = result;
        next();
      })
      .catch(error => {
        next(error);
      })
  },

  /**
   * 获取商品类目的信息
   * @param request
   * @param response
   * @param next
   */
  gGoodsCategory: (request, response, next) => {
    let start = response.start ? response.start : 0;
    let size = response.size ? response.size : 2147483647; // 即数据库中的int = 11, 最大位

    let {id, name_describe, username, root_category_id} = request.body;
    GoodsCategory.getGoodsCategory(start, size, id, name_describe, username, root_category_id)
      .then(result => {
        request.recordCountGoodsCategory = result;
        next();
      })
      .catch(error => {
        next(error);
      })
  },

}
